package com.framework.web.bus.service;

import com.framework.web.bus.domain.GoodsInfo;
import com.framework.web.bus.repository.GoodsInfoRepository;
import com.framework.web.bus.repository.specifications.GoodsInfoSpecs;
import com.framework.web.common.domain.BusinessConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author laowang
 * @version V1.0
 * @Title: Service
 * @Description: 业务层
 * @date 2016-09-11
 */
@Service
public class GoodsInfoService {

    @Autowired
    private GoodsInfoRepository goodsInfoRepository;

    /**
     * 新增或者保存
     *
     * @param goodsInfo
     * @return
     */
    public GoodsInfo save(GoodsInfo goodsInfo) {
        return goodsInfoRepository.save(goodsInfo);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void delete(String id) {
        GoodsInfo goodsInfo = goodsInfoRepository.findOne(id);
        goodsInfo.setStatus(BusinessConstants.STATUS.remove);
        goodsInfoRepository.save(goodsInfo);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public GoodsInfo findById(String id) {
        return goodsInfoRepository.findOne(id);
    }


    /**
     * 分页查找
     *
     * @param goodsInfo
     * @param pr
     * @return
     */
    public Page<GoodsInfo> getPageList(GoodsInfo goodsInfo, PageRequest pr) {
        Page<GoodsInfo> totalList = goodsInfoRepository.findAll(GoodsInfoSpecs.pageQueryGoodsInfo(goodsInfo), pr);
        return totalList;
    }

    /**
     * 根据商品类别Id查询有效的商品列表
     *
     * @param goodsTypeId
     * @return
     */
    public List<GoodsInfo> findByGoodsTypeId(String goodsTypeId) {
        return goodsInfoRepository.findByGoodsTypeIdAndStatus(goodsTypeId, BusinessConstants.STATUS.NORMAL);
    }

    /**
     * 查询商品类别中销量最高的商品信息
     * @param goodsTypeId
     * @return
     */
    public List<GoodsInfo> findByGoodsTypeIdOrderBySalesVolume(String goodsTypeId) {
        return goodsInfoRepository.findByGoodsTypeIdAndStatusOrderBySalesVolume(goodsTypeId,BusinessConstants.STATUS.NORMAL);
    }

}
